package com.axin.blog.controller;

import com.axin.blog.api.domain.BlogCategory;
import com.axin.blog.service.IBlogCategoryService;
import com.axin.common.core.domain.web.AjaxResult;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.security.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 博客分类 接口控制
 *
 * @Author axin
 * @Date 2021/10/29 17:10
 **/
@RestController
@RequestMapping("/category")
public class BlogCategoryController extends BaseController {

    @Autowired
    private IBlogCategoryService blogCategoryService;


    /**
     * 根据参数分页查询博客分类列表
     */
    @PreAuthorize("@ss.hasPermi('blog:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogCategory blogCategory) {
        return blogCategoryService.selectPageBlogCategoryList(blogCategory);
    }

    /**
     * 获取前端下拉可选分类列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('blog:category:list')")
    @GetMapping("/getCategoryList")
    public AjaxResult<List<BlogCategory>> list() {
        return AjaxResult.success(blogCategoryService.selectBlogCategoryList(new BlogCategory()));
    }

    /**
     * 新增分类
     */
    @PreAuthorize("@ss.hasPermi('blog:category:add')")
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody BlogCategory category) {
        if (!blogCategoryService.checkCategoryNameUnique(category)) {
            return AjaxResult.error("新增分类'" + category.getCategoryName() + "'失败，分类名已存在");
        }
        return toAjax(blogCategoryService.insertBlogCategory(category));
    }


    /**
     * 根据分类ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable Long categoryId) {
        return AjaxResult.success(blogCategoryService.selectBlogCategoryById(categoryId));
    }

    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('blog:category:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BlogCategory category) {
        if (!blogCategoryService.checkCategoryNameUnique(category)) {
            return AjaxResult.error("修改分类'" + category.getCategoryName() + "'失败，分类名已存在");
        }
        return toAjax(blogCategoryService.updateBlogCategory(category));
    }


    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('blog:category:remove')")
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        blogCategoryService.deleteCategoryByIds(categoryIds);
        return success();
    }

    /************************前台接口******************************************************************************************/

    /**
     * 获取分类归档
     *
     * @return
     */
    @GetMapping("/getWebCategoryList")
    public AjaxResult getWebCategoryList() {
        return AjaxResult.success(blogCategoryService.getCategoryArchiveList());
    }


}
